$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'cgs/cgsquestion/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '申请人id', name: 'applyUserId', index: 'apply_user_id', width: 80, hidden: true},
            {label: '申请人姓名', name: 'applyUserName', index: 'apply_user_name', width: 80},
            {label: '身份证号', name: 'idCardNum', index: 'id_card_num', width: 80},
            {label: '联系方式', name: 'mobile', index: 'mobile', width: 80},
            {label: '地址', name: 'address', index: 'address', width: 80},
            {label: '问题内容', name: 'questionContent', index: 'question_content', width: 80},
            {label: '问题相关图片', name: 'questionImg', index: 'question_img', width: 80, hidden: true},
            {label: '回复内容', name: 'answer', index: 'answer', width: 80},
            {label: '申请时间', name: 'createTime', index: 'create_time', width: 80},
            {label: '回复时间', name: 'answerTime', index: 'answer_time', width: 80},
            {
                label: '是否公众展示',
                name: 'isToAll',
                index: 'is_to_all',
                width: 80,
                formatter: function (value, options, rows) {
                    if (value === '0') {
                        return '<span>不向公众展示</span>';
                    } else if (value === '1') {
                        return '<span>向公众展示</span>';
                    }
                }
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        cgsQuestion: {},
        sfzs:[{"value":'0',"text":"不向公众展示"},{ "value":"1","text":"向公众展示"}],
        shenpi:{}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.cgsQuestion = {};
        },
        look: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "详情";

            vm.getInfo(id)
        },
        huifu: function () {
            var url = "cgs/cgsquestion/update";
            if($.isEmptyObject(vm.shenpi.answer)){
                alert("请填写回复内容");
                return ;
            }
            vm.shenpi.id=vm.cgsQuestion.id;
            vm.shenpi.answerTime= dateFtt('yyyy-MM-dd hh:mm:ss',new Date());
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.shenpi),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "cgs/cgsquestion/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "cgs/cgsquestion/info/" + id, function (r) {
                vm.cgsQuestion = r.cgsQuestion;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});